package ui.panels.primary.adaptiveAgents;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ui.flowManager.MissingData;
import ui.flowManager.expirimentProperties.ExperimentProperties;
import ui.flowManager.expirimentProperties.ExperimentProperties.Agent_Group;
import ui.flowManager.expirimentProperties.ExperimentProperties.Missing_Data_Error;
import ui.panels.commands.NextCommand;
import ui.panels.commands.OpenErrorCommand;
import auxiliary.AuxiliaryClass;

public class AdaptiveActionListener implements ActionListener{

	private AdaptiveAgentsPanel		myPanel = null;
	private ExperimentProperties	props = null;
	
	public AdaptiveActionListener(AdaptiveAgentsPanel panel, ExperimentProperties props)
	{
		this.myPanel = panel;
		this.props 	= props;
	}
	
	public void actionPerformed(ActionEvent event) {
		//Enter button
		if(event.getSource() ==  myPanel.getjButton1())
		{
			buttonEnter();
		}
		//Add Players
		else if(event.getSource() == myPanel.getAddPlayersButton())
		{
			buttonaddPlayers();
		}
		//Next
		else if (event.getSource() == myPanel.getNext())
		{
			buttonNext();
		}
	}
	
	
	private void buttonEnter()
	{
		String inputPlayers = myPanel.getNumPlayersField().getText();
		
		//case not attached to a game
		if(myPanel.getGameNumber() == 0)
			return;		
		//case not empty and is a number in both fields
		if((!inputPlayers.equals(""))
			&& (AuxiliaryClass.isNumber(inputPlayers)))
		{
			int numberPlayers = Integer.parseInt(inputPlayers);
			// update properties		
			props.setPlayers(myPanel.getGameNumber(), numberPlayers, Agent_Group.STUDIED_AGENT);						
			//empty the player list
			myPanel.getListModel().removeAllElements();
			//add the players to the list
			myPanel.setPlayers(numberPlayers);
		}	
	}	
	
	
	private void buttonaddPlayers()
	{
		//case the game not initialized 
		if((myPanel.getGameNumber() == 0) ||(!props.isInitialized(myPanel.getGameNumber())))
			return;
		
		String morePlayersStr = myPanel.getMorePlayersField().getText();		
		//case it is not number
		if (!AuxiliaryClass.isNumber(morePlayersStr))
			return;
		
		int morePlayers = Integer.parseInt(morePlayersStr);
		//add the players to the ExperementPropeties
		props.addPlayers(myPanel.getGameNumber(), morePlayers, Agent_Group.STUDIED_AGENT);
		//add the players to the list (GUI)
		myPanel.addPlayers(morePlayers);
	}
	
	private void buttonNext()
	{
		//show error in case of missing data
		if(props.whatMissing(Agent_Group.STUDIED_AGENT).getErrorType() != 
							Missing_Data_Error.NO_MISSING_DATA)
		{
			showMissingData(props.whatMissing(Agent_Group.STUDIED_AGENT)
									, Agent_Group.STUDIED_AGENT);
		}
		else if (props.whatMissing(Agent_Group.CONTROL_AGENT).getErrorType() != 
			Missing_Data_Error.NO_MISSING_DATA)
		{

			showMissingData(props.whatMissing(Agent_Group.CONTROL_AGENT)
									, Agent_Group.CONTROL_AGENT);
		}
		//case not missing data
		else
		{	
			NextCommand command = new NextCommand();
			command.execute();
		}
	}
	
	private void showMissingData(MissingData missingData, Agent_Group group)
	{
		String type = null;
		
		if(group == Agent_Group.STUDIED_AGENT)
			type = "Studied";
		else
			type = "Control";
		
		if (missingData.getErrorType() == Missing_Data_Error.PLAYER_NOT_INITIALIZED)
		{
			OpenErrorCommand command = new OpenErrorCommand();
			command.setMeesage(type + " Player " +missingData.getPlayerNumber() 
					+ " not initialized");
			
			command.execute();
		}
	}
}
